package com.dudu.log.statusLog.service.impl;

import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.log.statusLog.dao.StatusPushDao;
import com.dudu.log.statusLog.service.IStatusPushService;

import core.CoreDao;
import core.CoreDaoInvoke;
import core.CoreService;

public class StatusPushServiceImpl implements IStatusPushService {

    private CoreDao coreDao = CoreDaoInvoke.getInstance();
    private StatusPushDao statusPushDao = new StatusPushDao();

    /**
     * 查询条数（分页用）
     * 
     * @param paramSql
     * @return
     */
    public Integer getTotalAcount(String paramSql) {
	return statusPushDao.getTotalAcount(coreDao, paramSql);
    }

    /**
     * 查询数据（分页）
     * 
     * @param paramSql
     * @param pageSql
     * @return
     */
    public List<JSONObject> queryAll(String paramSql, String pageSql) {
	List<JSONObject> list = statusPushDao.queryAll(coreDao, paramSql, pageSql);
	if (null == list) {
	    return list;
	}
	for (JSONObject object : list) {
	    String ability_type = object.optString("vServiceType");
	    if(ability_type.equals("VoiceNotice")){
		ability_type="VOICE";
	    }
	    if(ability_type.equals("TalksV3")){
		ability_type="TalksV2";
	    }
	    AbilityTypeEnum ab=AbilityTypeEnum.getAbilityTypeEnum(ability_type);
	    String ability_typevalue="";
	    if(ab!=null){
		ability_typevalue= ab.getValue();
	    }
	    
	    if (StringUtils.isBlank(ability_typevalue)) {
		object.put("vServiceType", ability_type);
	    } else {
		object.put("vServiceType", ability_typevalue);
	    }
	}
	return list;
    }

    /**
     * 重推状态
     * 
     * @param id
     * @return
     */
    public int rePushStatus(String id) {
	int result = -1;
	if (StringUtils.isNotBlank(id)) {
	    JSONObject jsonData = statusPushDao.queryById(coreDao, id);
	    if (jsonData != null) {
		JSONObject pushData = new JSONObject();
		pushData.put("session_id", jsonData.optString("vSessionsId"));
		pushData.put("caller", jsonData.optString("vCallerNbr"));
		pushData.put("callee", jsonData.optString("vCalleeNbr"));
		pushData.put("call_state", jsonData.optString("vCallState"));
		pushData.put("stateTime", jsonData.optString("vStateTime"));
		pushData.put("appidentify", jsonData.optString("app_identifify"));
		pushData.put("app_callback_url", jsonData.optString("app_callback_url"));
		pushData.put("serviceType", jsonData.optString("vServiceType"));
		pushData.put("call_reason", jsonData.optString("vReason"));
		// pushData.put("cust_account", jsonData.optString("cc"));
		// pushData.put("msg_id", jsonData.optString("cc"));
		/**
		 * 把状态对象转成JSON,准备发送数据
		 */
		JSONObject postOject = new JSONObject();
		postOject.put("describe", "收到请返回success");
		postOject.put("data", pushData.toString());
		CoreService.log.info("推送状态数据URL=" + jsonData.optString("app_callback_url") + "req=" + postOject.toString());
		// 向第三方推送数据
		String response = com.dudu.utils.HttpRequest.postData(jsonData.optString("app_callback_url"), postOject.toString());
		CoreService.log.info("推送状态数据response=" + response);
		if (StringUtils.isNotBlank(response)) {
		    result = 1;
		    // 更改推送结束为成功
		    statusPushDao.updateStatusLogCode(coreDao, jsonData);
		}
	    }
	}

	return result;
    }

}
